﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SolutionGenerator
{
    public partial class WizardProcessFKChilds : UserControlParent
    {
        public WizardProcessFKChilds(SolutionConfiguration configuration) : base(configuration)
        {
            InitializeComponent();
        }
        public override void Save()
        {
            foreach (string tableName in SolutionGeneratorListOfTables.CheckedItems)
            {
                SolutionConfigurationTableInfo table = GetSolutionConfiguration.GetTableInfo.Single(_ => _.TableName == tableName);
                table.IsFKChildProcess = true;
            }
        }
        public override void LoadWizard()
        {
            SolutionGeneratorListOfTables.Items.Clear();
            foreach (SolutionConfigurationTableInfo table in GetTableWithFKChilds())
                SolutionGeneratorListOfTables.Items.Add(table.TableName);
            GetSelectedTable();
        }
        public override string WizardTitle
        {
            get { return TextMessage.WizardTitle_ProcessFKChilds; }
        }
        private List<SolutionConfigurationTableInfo> GetTableWithFKChilds()
        {
            return GetSolutionConfiguration.GetTableInfo.Where(_ => _.GetColumnInfo.
                Count(j => j.FKList != null && _.IsProcess && j.FKList.
                    Count(k_ => k_.FKType == ForeignKeyType.Child) > 0) > 0).ToList();
        }
        private void GetSelectedTable()
        {
            for (int i = 0; i < SolutionGeneratorListOfTables.Items.Count; i++)
                SolutionGeneratorListOfTables.SetItemChecked(i, GetSolutionConfiguration.
                    GetTableInfo.Count(_ => _.TableName == (string)SolutionGeneratorListOfTables.Items[i] && _.IsFKChildProcess) > 0);
        }
        private void SolutionGeneratorGetTableSelectAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < SolutionGeneratorListOfTables.Items.Count; i++)
                SolutionGeneratorListOfTables.SetItemChecked(i, SolutionGeneratorGetTableSelectAll.Checked);
        }
    }
}